package com.tf.temper.controller;


import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.*;
import com.tf.temper.service.DeviceService;
import com.tf.temper.service.LoghbService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 设备心跳接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "设备心跳接口",description="HeartbeatController")
@RestController
@RequestMapping("/syheartbeat")
public class HeartbeatController extends BaseController
{
    @Autowired
    private DeviceService<DeviceEntity> deviceService;
    @Autowired
    private LoghbService<LoghbEntity> loghbService;
    /**
     * 设备心跳接口
     */
    @ApiOperation("设备心跳接口")
    @PostMapping("/{deviceid}")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public DeviceReturnEntity<String> SetHeartbeat(@PathVariable("deviceid") String deviceid,@RequestBody HeadbeatuploadEntity headentity)
    {
        EntityWrapperHandle<DeviceEntity> ew = new EntityWrapperHandle<DeviceEntity>();
        ew.eq("deviceid", deviceid);
        DeviceEntity deventity =  deviceService.selectOne(ew);
        if(null != deventity)
        {
            LoghbEntity loghbEntity = new LoghbEntity();
            loghbEntity.setDeviceid(deviceid);
            loghbEntity.setServerurl(headentity.getHeartinfo().getServerURL());
            loghbService.insert(loghbEntity);
        }
        else
         {
             return  new DeviceReturnEntity<String>().error("device no register");
         }
        return  new DeviceReturnEntity<String>().ok("0");
    }
}